/*!
 * @file Viewer.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2015 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooViewer/Viewer>
#include <ooViewer/SceneHandler>

#include <osgODE/Notify>
#include <osgODE/ThreadedManager>

#include <osgODEUtil/FindObjects>

#include <osgDB/Registry>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooViewer ;




/* ======================================================================= */
/* ....................................................................... */
Viewer::Viewer(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Viewer::Viewer(const Viewer& other, const osg::CopyOp& copyop):
    osgViewer::Viewer   ( other, copyop )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Viewer::Viewer( osg::ArgumentParser& arguments ):
    osgViewer::Viewer   ( arguments )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
Viewer::~Viewer(void)
{
    setSceneData( NULL ) ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
int
Viewer::run(void)
{
    // init ode

    dInitODE2(0) ;
    dAllocateODEDataForThread( (unsigned int) dAllocateMaskAll ) ;



#if 0
    if (!getCameraManipulator() && getCamera()->getAllowEventFocus())
    {
        setCameraManipulator(new osgGA::TrackballManipulator());
    }
#endif

    setReleaseContextAtEndOfFrameHint(false);

    return this->osgViewer::ViewerBase::run();
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Viewer::frame( double simulationTime )
{
    this->osgViewer::Viewer::frame( simulationTime ) ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
Viewer::setSceneData( osg::Node* graph )
{
    // back to osgViewer
    this->osgViewer::Viewer::setSceneData( graph ) ;
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
bool
Viewer::loadSceneData(  std::istream& data, const std::string& extension  )
{
    PS_DBG("ooViewer::Viewer::loadSceneData(%p, ...)", this) ;


    osg::ref_ptr<osgDB::ReaderWriter>   rw = osgDB::Registry::instance()->getReaderWriterForExtension(extension) ;
    PS_ASSERT1( rw.valid() ) ;

    if( ! rw.valid() ) {
        PS_FATAL("ooViewer::Viewer::loadSceneData(%p, ...): cannot find reader for \"%s\"", this, extension.c_str()) ;
        return false ;
    }



    osgDB::ReaderWriter::ReadResult rr = rw->readNode(data) ;

    if( rr.status() != osgDB::ReaderWriter::ReadResult::FILE_LOADED ) {
        PS_FATAL("ooViewer::Viewer::loadSceneData(%p, ...): error while reading from stream", this) ;
        return false ;
    }


    setSceneData( rr.takeNode() ) ;

    return true ;
}
/* ....................................................................... */
/* ======================================================================= */
